using System;
using System.Collections.Generic;

namespace WenYu.Core.Parser;

/// <summary>
/// 语句节点基类
/// </summary>
public abstract class Statement : AstNode
{
}

/// <summary>
/// 程序根节点
/// </summary>
public class Program : AstNode
{
    public List<Statement> Statements { get; set; }
    
    public Program()
    {
        Statements = new List<Statement>();
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
}

/// <summary>
/// 语句块
/// </summary>
public class BlockStatement : Statement
{
    public List<Statement> Statements { get; set; }
    
    public BlockStatement()
    {
        Statements = new List<Statement>();
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
}

/// <summary>
/// 变量声明语句
/// </summary>
public class VarDeclStatement : Statement
{
    public string Type { get; set; }        // "整型", "实型", "文字", "真假", "auto"
    public string Name { get; set; }
    public Expression? Init { get; set; }   // 初始值（可选）
    
    public VarDeclStatement(string type, string name, Expression? init = null)
    {
        Type = type;
        Name = name;
        Init = init;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => Init == null ? 
        $"{Type} {Name}。" : 
        $"{Type} {Name} 是 {Init}。";
}

/// <summary>
/// 数组声明语句
/// </summary>
public class ArrayDeclStatement : Statement
{
    public string ElementType { get; set; }  // 元素类型：整型、实型等
    public string Name { get; set; }
    public Expression? Init { get; set; }    // 初始化（ArrayLiteral或null）
    public Expression? Length { get; set; }  // 长度表达式（可选）
    
    public ArrayDeclStatement(string elementType, string name, Expression? init = null, Expression? length = null)
    {
        ElementType = elementType;
        Name = name;
        Init = init;
        Length = length;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => $"{ElementType}组 {Name}";
}

/// <summary>
/// 赋值语句
/// </summary>
public class AssignmentStatement : Statement
{
    public Expression Target { get; set; }   // 可以是变量、数组元素、属性
    public Expression Value { get; set; }
    
    public AssignmentStatement(Expression target, Expression value)
    {
        Target = target;
        Value = value;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => $"{Target} 是 {Value}。";
}

/// <summary>
/// 表达式语句（函数调用等）
/// </summary>
public class ExpressionStatement : Statement
{
    public Expression Expression { get; set; }
    
    public ExpressionStatement(Expression expression)
    {
        Expression = expression;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => $"{Expression}。";
}

/// <summary>
/// 条件语句（若...则...余）
/// </summary>
public class IfStatement : Statement
{
    public Expression Condition { get; set; }
    public BlockStatement ThenBlock { get; set; }
    public BlockStatement? ElseBlock { get; set; }
    
    public IfStatement(Expression condition, BlockStatement thenBlock, BlockStatement? elseBlock = null)
    {
        Condition = condition;
        ThenBlock = thenBlock;
        ElseBlock = elseBlock;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString()
    {
        var result = $"若 {Condition} 则{{ ... }}";
        if (ElseBlock != null)
            result += " 余{ ... }";
        return result;
    }
}

/// <summary>
/// While循环语句
/// </summary>
public class WhileStatement : Statement
{
    public Expression Condition { get; set; }
    public BlockStatement Body { get; set; }
    public bool IsDoWhile { get; set; }  // false: while, true: do-while
    
    public WhileStatement(Expression condition, BlockStatement body, bool isDoWhile = false)
    {
        Condition = condition;
        Body = body;
        IsDoWhile = isDoWhile;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => IsDoWhile ? 
        "复{ ... }若 条件 续。" : 
        "若 条件 复{ ... }";
}

/// <summary>
/// For循环语句（计数循环）
/// </summary>
public class ForStatement : Statement
{
    public string Variable { get; set; }     // 循环变量名
    public Expression Start { get; set; }    // 起始值
    public Expression End { get; set; }      // 结束值
    public Expression Step { get; set; }     // 步长
    public BlockStatement Body { get; set; }
    
    public ForStatement(string variable, Expression start, Expression end, Expression step, BlockStatement body)
    {
        Variable = variable;
        Start = start;
        End = end;
        Step = step;
        Body = body;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => $"复 {Variable} 从 {Start} 至 {End} 步 {Step} {{ ... }}";
}

/// <summary>
/// ForEach循环语句（数组遍历）
/// </summary>
public class ForEachStatement : Statement
{
    public Expression Array { get; set; }    // 数组表达式
    public string Variable { get; set; }     // 元素变量名
    public BlockStatement Body { get; set; }
    
    public ForEachStatement(Expression array, string variable, BlockStatement body)
    {
        Array = array;
        Variable = variable;
        Body = body;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => $"复 {Array} 取 {Variable} {{ ... }}";
}

/// <summary>
/// Break语句（停）
/// </summary>
public class BreakStatement : Statement
{
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => "停。";
}

/// <summary>
/// Continue语句（跳）
/// </summary>
public class ContinueStatement : Statement
{
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => "跳。";
}

/// <summary>
/// Return语句
/// </summary>
public class ReturnStatement : Statement
{
    public Expression? Value { get; set; }
    public bool HasValue { get; set; }
    
    public ReturnStatement(Expression? value, bool hasValue)
    {
        Value = value;
        HasValue = hasValue;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => HasValue ? $"得 {Value}。" : "回。";
}

/// <summary>
/// 函数参数
/// </summary>
public class Parameter
{
    public string Type { get; set; }
    public string Name { get; set; }
    
    public Parameter(string type, string name)
    {
        Type = type;
        Name = name;
    }
    
    public override string ToString() => $"{Type} {Name}";
}

/// <summary>
/// 函数定义语句
/// </summary>
public class FunctionDeclaration : Statement
{
    public string? ReturnType { get; set; }   // null表示无返回值
    public string Name { get; set; }
    public List<Parameter> Parameters { get; set; }
    public BlockStatement Body { get; set; }
    
    public FunctionDeclaration(string? returnType, string name, List<Parameter> parameters, BlockStatement body)
    {
        ReturnType = returnType;
        Name = name;
        Parameters = parameters;
        Body = body;
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString()
    {
        var params_str = string.Join("，", Parameters);
        var return_part = ReturnType != null ? $"求 {ReturnType} 之 " : "求 ";
        return $"{params_str} {return_part}法 {Name} {{ ... }}";
    }
}

/// <summary>
/// 类字段
/// </summary>
public class Field
{
    public string Type { get; set; }
    public string Name { get; set; }
    public Expression? DefaultValue { get; set; }
    
    public Field(string type, string name, Expression? defaultValue = null)
    {
        Type = type;
        Name = name;
        DefaultValue = defaultValue;
    }
    
    public override string ToString() => DefaultValue != null ? 
        $"{Type} {Name} 是 {DefaultValue}。" : 
        $"{Type} {Name}。";
}

/// <summary>
/// 类方法（成员函数）
/// </summary>
public class ClassMethod
{
    public string? ReturnType { get; set; }   // null表示无返回值
    public string Name { get; set; }
    public List<Parameter> Parameters { get; set; }
    public BlockStatement Body { get; set; }
    
    public ClassMethod(string? returnType, string name, List<Parameter> parameters, BlockStatement body)
    {
        ReturnType = returnType;
        Name = name;
        Parameters = parameters;
        Body = body;
    }
    
    public override string ToString()
    {
        var params_str = string.Join("，", Parameters);
        var return_part = ReturnType != null ? $"求 {ReturnType} 之 " : "求 ";
        return $"{params_str} {return_part}法 {Name} {{ ... }}";
    }
}

/// <summary>
/// 类定义语句
/// </summary>
public class ClassDeclaration : Statement
{
    public string Name { get; set; }
    public List<Field> Fields { get; set; }
    public List<ClassMethod> Methods { get; set; }
    
    public ClassDeclaration(string name, List<Field> fields, List<ClassMethod>? methods = null)
    {
        Name = name;
        Fields = fields;
        Methods = methods ?? new List<ClassMethod>();
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => $"{Name} 是 类，含{{ {Fields.Count}个属性, {Methods.Count}个方法 }}";
}

/// <summary>
/// 属性初始化器（用于对象创建时初始化属性）
/// </summary>
public class PropertyInitializer
{
    public string Name { get; set; }
    public Expression Value { get; set; }
    
    public PropertyInitializer(string name, Expression value)
    {
        Name = name;
        Value = value;
    }
    
    public override string ToString() => $"{Name} 是 {Value}";
}

/// <summary>
/// 对象创建语句
/// </summary>
public class ObjectCreationStatement : Statement
{
    public string ClassName { get; set; }
    public string ObjectName { get; set; }
    public List<PropertyInitializer> Initializers { get; set; }
    
    public ObjectCreationStatement(string className, string objectName, List<PropertyInitializer>? initializers = null)
    {
        ClassName = className;
        ObjectName = objectName;
        Initializers = initializers ?? new List<PropertyInitializer>();
    }
    
    public override T Accept<T>(IAstVisitor<T> visitor)
    {
        throw new NotImplementedException();
    }
    
    public override string ToString() => Initializers.Count > 0 
        ? $"{ClassName} {ObjectName}，含{{ {Initializers.Count}个属性 }}" 
        : $"{ClassName} {ObjectName}。";
}

